package org.mspring.mlog.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mspring.mlog.entity.Message;
import org.mspring.mlog.entity.MessageText;
import org.mspring.mlog.entity.security.User;
import org.mspring.mlog.service.MessageService;
import org.mspring.platform.dao.hibernate.BaseDao;
import org.mspring.platform.utils.FreemarkerUtils;
import org.mspring.platform.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月22日
 */
@Service
@Transactional
public class MessageServiceImpl extends BaseDao implements MessageService {

    private static final Logger log = Logger.getLogger(MessageServiceImpl.class);

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Override
    public boolean sendMessage(Long sender, Long receiver, String content) {
        // TODO Auto-generated method stub
        try {
            MessageText text = new MessageText();
            text.setContent(content);
            text.setCreateTime(new Date());

            Message msg = new Message();
            if (sender != null) {
                msg.setSender(new User(sender));
            }
            msg.setReceiver(new User(receiver));
            msg.setMessageText(text);
            msg.setReaded(false);
            save(msg);
            log.debug("send message success. sender = " + sender + ", receiver = " + receiver + ", content = " + content);
            return true;
        } catch (Exception e) {
            // TODO: handle exception
            log.error("send message success. sender = " + sender + ", receiver = " + receiver + ", content = " + content);
            return false;
        }
    }

    @Override
    public boolean sendMessage(Long sender, Long receiver, String ftl, Map<Object, Object> model) {
        // TODO Auto-generated method stub
        String content = FreemarkerUtils.render(freeMarkerConfigurer.getConfiguration(), ftl, model);
        if (StringUtils.isNotBlank(content)) {
            return sendMessage(sender, receiver, content.trim());
        } else {
            return false;
        }
    }

    @Override
    public Integer getUnReadMessageCount(Long user) {
        // TODO Auto-generated method stub
        return count("select count(*) from Message msg where msg.readed = ? and msg.receiver.id = ?", false, user);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Message> getUnReadMessage(Long user) {
        // TODO Auto-generated method stub
        return list("select msg from Message msg where msg.readed = ? and msg.receiver.id = ?", false, user);
    }

    @Override
    public void setReadedMessage(Long user) {
        // TODO Auto-generated method stub
        executeUpdate("update Message m set m.readed = ? where m.receiver.id = ?", true, user);
    }

    @Override
    public void deleteMessage(Long user, Long message) {
        // TODO Auto-generated method stub
        executeUpdate("delete from Message m where m.id = ? and m.receiver.id = ?", message, user);
    }

}
